package edu.mju.scenic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.scenic.entity.Attractions;
import edu.mju.scenic.entity.Celebrity;
import edu.mju.scenic.service.AttractionsService;
import edu.mju.serviceutils.ResponseCode;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBL
 * @since 2020-12-28
 */
@RestController //标识Controller
@RequestMapping("/scenic/attractions") //映射请求
//@CrossOrigin  //解决跨域问题
public class AttractionsController {

    @Autowired  //注入接口
    private AttractionsService attractionsService;

    /**
     * 根据根据景区id 新增/修改景区名胜
     * @param attractions 新增信息
     * @return 自定义返回
     */
    @PostMapping("/updateSpotAttractions")  //表明增加的URL
                                           //表明json请求体，前段的数据体转换为Attractions
    public ResponseCode updateSpotAttractions(@RequestBody Attractions attractions){
                                         //框架封装方法，自动检测ID
        boolean save = attractionsService.saveOrUpdate(attractions);
        if(save){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }

    /**
     * 根据名胜id删除名胜
     * @param id 名胜id
     * @return 自定义返回
     */
    //表明是一个删除的URL
    @DeleteMapping("/deleteSpotAttractions/{id}")
                                              //对参数的添加元数据                                            接收请求路径中占位符的值
    public ResponseCode deleteSpotAttractions(@ApiParam(name = "id" ,value = "删除名胜id" ,required = true) @PathVariable Integer id){
        boolean delete = attractionsService.deleteByAttractionsId(id);
        return ResponseCode.success();
    }

    /**
     * 根据景点id获取名胜
     * @param spotId 景点id
     * @return 自定义返回
     */
    //表明是一个查询的url
    @GetMapping("/getSpotAttractions/{spotId}")
                                       //对参数的添加元数据                                            接收请求路径中占位符的值
    public ResponseCode getSpotAttractions(@ApiParam(name = "spotId" ,value = "根据景点id",required = true) @PathVariable Integer spotId){
        List<Attractions> list = attractionsService.listBySpotId(spotId);
        return ResponseCode.success().data("attractions",list);
    }

    /**
     * 分页获取名胜
     * @param page 当前页
     * @param size 条数
     * @return 自定义返回
     */
    //表明是一个查询的url
    @GetMapping("/getAllAttractions/{page}/{size}")

    public ResponseCode getAllAttractions(@PathVariable Long page,
                                          @PathVariable Long size) {
        IPage<Attractions> attractionsIPage = new Page<>(page, size);
        Map<String, Object> map = attractionsService.getCelebrity(attractionsIPage);
        return ResponseCode.success().data(map).data("page",page);
    }

}

